// Declare app level module which depends on filters, and services

var _COMMON = {};
var _pageFn = {};
var _pageObj = {};
var _pageData = {};
var _viewData = {};

_COMMON.context = 'service/ws/';
_COMMON.token = '1234';
_COMMON._isLoading = false;

_COMMON.renderSlide = function () {

    $('#bxslider').bxSlider({
            mode: 'fade',
            auto: true,
            pause: 4000}
    );
//    $('#bxslider').
//
};

_COMMON.renderMenu = function () {

    var nav = $('#cssmenu');

    $(window).scroll(function () {
        if ($(this).scrollTop() > 180) {
            nav.addClass("f-nav");
        } else {
            nav.removeClass("f-nav");
        }
    });

};

_COMMON.validated = function () {

    var valid = true;
     var errormsg = '** This field is required!';
    var errorcn = 'error';

    $('.' + errorcn).remove();

     $('.required').each(function () {

        var parent = $(this).parent();

        if ($(this).val() == '') {
        
	    var msg = $(this).attr('regtext');
	    msg = (msg != '') ? msg : errormsg;

	    var html = '<span class="' + errorcn + '">' + errormsg + '</span>';
	    $(html).appendTo(parent).fadeIn('fast').click(function() {
		$(this).remove();
	    });

            valid = false;
        }
    });

    return valid;
};

var _log = function (msg) {
    console.log(msg);
};

//TODO refactor later
_COMMON.setPageData = function (value) {
    sessionStorage.setItem('_pageData', JSON.stringify(value));
};
_COMMON.getPageData = function () {
    return JSON.parse(sessionStorage.getItem('_pageData'));
};

_COMMON.loading = function (time, mode) {
//    var t = 3000;
    if (time) {
//        t = time;
        setTimeout(function () {
            $('#confirm').spin('modal');
        }, time);
    }

    if (null == mode || 1 == mode) {
	mode = 'start';
    }

    $('#confirm').spin('modal', mode);

   
    //    _log(t);

};

_COMMON.enum = {
    status : {
	'1': {color : 'rgb(0, 218, 0)' , value :'Active'},
	'0': {color : 'red', value :'Inactive'}	
    },
    userType : {
	'1': 'Administrator'	,
	'2': 'Content Manager'	,
	'3': 'Member'	
    },
    userTypes : [ {
	key : '1',
	value : 'Administrator'
    }, {
	key : '2',
	value : 'Content Manager'
    }, {
	key : '3',
	value : 'Member'
    } ]
};


jQuery(function ($) {

    $(window).ready(function () {
        _log('### Ready Stage ###');

        _COMMON.winHeight = $(document).height();
        _COMMON.winWidth = $(window).width();
        

    });

    $(window).load(function () {
        _log('### Load Stage ###');
    });

});

var default_opts = {
    lines: 9,
    length: 2,
    width: 3,
    radius: 8,
    corners: 1,
    rotate: 0,
    color: '#555',
    speed: 1,
    trail: 47,
    shadow: false,
    hwaccel: false,
    className: 'spinner',
    zIndex: 2e9,
    top: '50%',
    left: '50%'
};

var modal_opts = {
    lines: 11, // The number of lines to draw
    length: 23, // The length of each line
    width: 8, // The line thickness
    radius: 40, // The radius of the inner circle
    corners: 1, // Corner roundness (0..1)
    rotate: 9, // The rotation offset
    color: '#FFF', // #rgb or #rrggbb
    speed: 1, // Rounds per second
    trail: 50, // Afterglow percentage
    shadow: true, // Whether to render a shadow
    hwaccel: false, // Whether to use hardware acceleration
    className: 'spinner', // The CSS class to assign to the spinner
    zIndex: 2e9, // The z-index (defaults to 2000000000)
    top: '50%', // Top position relative to parent in px
    left: '50%' // Left position relative to parent in px
};

//jQuery extension
$.fn.spin = function (opts, action) {
    if (opts == null)
        opts = default_opts;

    if (opts == "modal")
        opts = modal_opts;

    this.each(function () {
        var $this = $(this),
            data = $this.data();

        if (data.spinner) {
            data.spinner.stop();
            delete data.spinner;
            if (opts == modal_opts)
                $("#spin_modal_overlay").remove();
            return this;
        }
        
        if (action == "start") {
            var spinElem = this;
            if (opts == modal_opts) {
                $('body').append('<div id="spin_modal_overlay" style="background-color: rgba(0, 0, 0, 0.6); width:100%; height:100%; position:fixed; top:0px; left:0px; z-index:' + (opts.zIndex - 1) + '"/>');
                spinElem = $("#spin_modal_overlay")[0];
            }
            data.spinner = new Spinner($.extend({ color: $this.css('color') }, opts)).spin(spinElem);
        }

    });

    return this;
};
